import Vue from 'vue'
import VueRouter from 'vue-router'
import UseLogin from '@/components/UseLogin'
Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    redirect:'/login'
  },
  {
    path:'/login',
    component:UseLogin,
    meta:{title:'登录页'}
  },
  {
    path:'/home',
    name:'home',
    component:() =>import('@/views/UseHome.vue'),
    // redirect:'/show',
    meta:{isAuth:true,title:'主页'},
    children:[
      {path:'/show',component:()=>import('@/components/ShowMain') },
      {path:'/users',component:()=>import('@/components/ShowUsers')},
      {path:'/roles',component:()=>import('@/components/authority/UseRoles')},
      {path:'/rights',component:()=>import('@/components/authority/UseRights')},
      {path:'/categories',component:()=>import('@/components/goods/UseCategory')},
      {path:'/goods',component:()=>import('@/components/goods/UseGoodsList')},
      {path:'/params',component:()=>import('@/components/goods/UseGoodsParams')},
      {path:'/goods/add',component:()=>import('@/components/goods/UseAddGoods')},
      {path:'/orders',component:()=>import('@/components/order/UseGoodsOrder')},
      {path:'/reports',component:()=>import('@/components/report/UseReport')}
    ]
  }
]  

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to,from,next) =>{
  //判断是否需要权限验证
   if (to.meta.isAuth) {
     let token = sessionStorage.getItem('token')
     //判断是否登录成功,只有登录成功之后才会将token存到sessionStorage
     if (token) {
       next()
     }else {
       //如果没有token,说明经过登录就访问其他页面(通过路径直接跳转其他页面),直接强制跳回登录页
       next('/login')
     }
   } else {
     next()
   }
})
router.afterEach((to,from) =>{
   
})
export default router
